package com.cskaoyan.service;

import com.cskaoyan.bean.promotion.Coupon;
import com.cskaoyan.bean.promotion.CouponExample;
import com.cskaoyan.bean.promotion.CouponUser;
import com.cskaoyan.bean.promotion.CouponUserExample;
import com.cskaoyan.bean.promotion.pojo.SimpleGroupon4Wx;
import com.cskaoyan.mapper.CouponMapper;
import com.cskaoyan.mapper.CouponUserMapper;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @Author: Li Qing
 * @Create: 2020/4/28 15:44
 * @Version: 1.0
 */
@Service
public class CouponServiceImpl implements CouponService {
    @Autowired
    CouponMapper couponMapper;
    @Autowired
    CouponUserMapper couponUserMapper;

    @Override
    public List<Coupon> queryCouponList(String name, Short status, Short type, Integer page, Integer limit, String sort, String order) {
        PageHelper.startPage(page, limit);
        PageHelper.orderBy(sort + " " + order);
        CouponExample example = new CouponExample();
        CouponExample.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(name)) criteria.andNameLike("%" + name + "%");
        if (status != null) criteria.andStatusEqualTo(status);
        if (type != null) criteria.andTypeEqualTo(type);
        criteria.andDeletedEqualTo(false);
        return couponMapper.selectByExample(example);
    }

    @Override
    public Integer updateCoupon(Coupon coupon) {
        coupon.setUpdateTime(new Date());
        return couponMapper.updateByPrimaryKeySelective(coupon);
    }

    @Override
    public Integer createCoupon(Coupon coupon) {

        Date date = new Date();
        coupon.setAddTime(date);
        coupon.setUpdateTime(date);
        return couponMapper.insertSelective(coupon);
    }

    @Override
    public Integer deleteCoupon(Coupon coupon) {
        coupon.setDeleted(true);
        coupon.setUpdateTime(new Date());
        return couponMapper.updateByPrimaryKeySelective(coupon);
    }

    @Override
    public Coupon queryCouponById(Integer id) {
        return couponMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<CouponUser> queryCouponUserList(Integer couponId, Integer userId, Short status, Integer page, Integer limit, String sort, String order) {
        PageHelper.startPage(page, limit);
        PageHelper.orderBy(sort + " " + order);
        CouponUserExample example = new CouponUserExample();
        CouponUserExample.Criteria criteria = example.createCriteria();
        if (couponId != null) criteria.andCouponIdEqualTo(couponId);
        if (userId != null) criteria.andUserIdEqualTo(userId);
        if (status != null) criteria.andStatusEqualTo(status);
        criteria.andDeletedEqualTo(false);
        return couponUserMapper.selectByExample(example);
    }

    //========================微信小程序接口分割线================================//
    @Override
    public List<SimpleGroupon4Wx> queryCouponList4Wx() {
        PageHelper.startPage(1,2);
        PageHelper.orderBy("add_time desc");
        return couponMapper.selectCouponList4Wx();
    }

    @Override
    public List<Coupon> queryCouponUser4Wx(Integer status,Integer page,Integer size,Integer userId){
        PageHelper.startPage(page, size);
        CouponUserExample example = new CouponUserExample();
        CouponUserExample.Criteria criteria = example.createCriteria();
        if (status != null) criteria.andStatusEqualTo(Short.parseShort(status+""));
        criteria.andDeletedEqualTo(false);
        if (userId != null) criteria.andUserIdEqualTo(userId);
        criteria.andDeletedEqualTo(false);
        List<CouponUser> couponUsers = couponUserMapper.selectByExample(example);
        List<Coupon> couponArray = new ArrayList<Coupon>();
        for (CouponUser c:couponUsers) {
            Coupon coupon = couponMapper.selectByPrimaryKey(c.getCouponId());
            couponArray.add(coupon);
        }
        return couponArray;
    }

    @Override
    public List<Coupon> queryCouponUserList4Wx(Integer page,Integer size,Integer id){
        PageHelper.startPage(page, size);
        CouponUserExample example = new CouponUserExample();
        CouponExample example11=new CouponExample();
        CouponUserExample.Criteria criteria = example.createCriteria();
        if (id != null) criteria.andUserIdEqualTo(id);
        criteria.andDeletedEqualTo(false);
        List<CouponUser> couponUsers = couponUserMapper.selectByExample(example);
        List<Coupon> coupons = couponMapper.selectByExample(example11);
        List<Coupon> coupons1=new ArrayList<>(coupons);
        int []hascouId=new int[couponUsers.size()];
        for(int i=0;i<couponUsers.size();i++){
            hascouId[i]=couponUsers.get(i).getCouponId();
        }
        for (Coupon c:coupons) {
            if (!Arrays.asList(hascouId).contains(c.getId()+""));
            coupons1.add(c);
        }
        return coupons1;
    }

    @Override
    public List<Coupon> selectCouponList(Integer cartId, Integer userId) {
        return couponMapper.selectCoupon4Wx(cartId,userId);
    }

    @Override
    public Integer createCouponUser(Integer id, Integer userId) {
        Coupon coupon = couponMapper.selectByPrimaryKey(id);
        CouponUser couponUser = new CouponUser();
        couponUser.setUserId(userId);
        couponUser.setCouponId(coupon.getId());
        couponUser.setStatus(coupon.getStatus());
        couponUser.setStartTime(coupon.getStartTime());
        couponUser.setEndTime(coupon.getEndTime());
        couponUser.setAddTime(new Date());
        couponUser.setDeleted(false);
        return couponUserMapper.insert(couponUser);
    }

    @Override
    public Integer createCouponUserByCode(String code, Integer userId) {
        CouponExample example = new CouponExample();
        CouponExample.Criteria criteria = example.createCriteria();
        if (code != null) criteria.andCodeEqualTo(code);
        criteria.andDeletedEqualTo(false);
        List<Coupon> coupons = couponMapper.selectByExample(example);
        for (Coupon c:coupons ) {
            c.setDeleted(true);
            couponMapper.updateByPrimaryKeySelective(c);
            CouponUser couponUser = new CouponUser();
            couponUser.setUserId(userId);
            couponUser.setCouponId(c.getId());
            couponUser.setStatus(c.getStatus());
            couponUser.setStartTime(c.getStartTime());
            couponUser.setEndTime(c.getEndTime());
            couponUser.setAddTime(new Date());
            couponUser.setUpdateTime(new Date());
            couponUser.setDeleted(false);
            couponUserMapper.insert(couponUser);
        }
        return 0;
    }
}
